﻿#include "txr_algo_dlm_cls_v2.h"
#include "infer_cls_v2.h"

namespace txr_algo_dlm_cls
{
	TxrAlgoDlmClsV2::TxrAlgoDlmClsV2()
		:m_p_infer(TK_NULL_PTR)
	{

	}

	TxrAlgoDlmClsV2::~TxrAlgoDlmClsV2()
	{
		Release();
	}

	tk_bool TxrAlgoDlmClsV2::Load(st_dlm_cfg& cfg)
	{
		Release();
		m_p_infer = new InferClsV2();

		std::string path_weight = std::string(cfg.file_path_name) + ".cls2";

		return m_p_infer->Init(cfg.gpu_id, path_weight.c_str());
	}

	void TxrAlgoDlmClsV2::Release()
	{
		SAFE_DELETE(m_p_infer);
	}

	tk_int32 TxrAlgoDlmClsV2::BatchSize()
	{
		return m_p_infer->BatchSize();
	}

	void TxrAlgoDlmClsV2::Detect(st_detect_unit* p_unit, tk_int32 size)
	{
		if (!size || !m_p_infer)
		{
			return;
		}
		for (tk_int32 i = 0; i < size; ++i)
		{
			st_img_rgb& rgb = p_unit[i].img;
			if (!
				((rgb.pp_rgb8[0] && rgb.pp_rgb8[1] && rgb.pp_rgb8[2]) ||
					(rgb.pp_rgb16[0] && rgb.pp_rgb16[1] && rgb.pp_rgb16[2]) ||
					(rgb.pp_rgbf[0] && rgb.pp_rgbf[1] && rgb.pp_rgbf[2]))
				)
			{
				Q_ASSERT(0);
				return;
			}
		}

		m_p_infer->Detect(p_unit, size);
	}
	//===========================================
	extern "C" TXR_ALGO_DLM_CLS_V2_EXPORT TxrAlgoDlmClsImpl * CreateDlmClsObj();
	extern "C" TXR_ALGO_DLM_CLS_V2_EXPORT void DeleteDlmClsObj(TxrAlgoDlmClsImpl * p);
	TxrAlgoDlmClsImpl* CreateDlmClsObj()
	{
		return new TxrAlgoDlmClsV2();
	}

	void DeleteDlmClsObj(TxrAlgoDlmClsImpl* p)
	{
		SAFE_DELETE(p);
	}
}//namespace txr_algo_dlm